<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeFn"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          node-key="id"
          :expand-on-click-node="false"
          :highlight-current="true"
          @node-click="handleNodeClick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table
          :data="tableData"
          style="width: 100%"
        >
          <el-table-column
            prop="staffPhoto"
            label="头像"
            width="100px"
          >
            <!-- 当使用自定义内容时，写入默认插槽 -->
            <template v-slot="{row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="username"
            label="姓名"
            width="100px"
          />
          <el-table-column
            prop="mobile"
            label="手机号"
            sortable
            width="100px"
          />
          <el-table-column
            prop="workNumber"
            label="工号"
            sortable
            width="100px"
          />
          <el-table-column
            prop="name"
            label="聘用形式"
            width="100px"
          >
            <template v-slot="{row}">
              <span>{{ row.formOfEmployment==='1'?'正式':row.formOfEmployment==='2'?'非正式':'无' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="departmentName"
            label="部门"
            width="100px"
          />
          <el-table-column
            prop="timeOfEntry"
            label="入职时间"
            sortable
            width="100px"
          />
          <el-table-column
            prop="address"
            label="操作"
          >
            <el-button type="text" size="mini">查看</el-button>
            <el-button type="text" size="mini">角色</el-button>
            <el-button type="text" size="mini">删除</el-button>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row align="middle" type="flex" justify="end" style="height:60px">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :page-size="queryParams.pagesize"
            :current-page.sync="queryParams.page"
            @current-change="getEmployee"
          />
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartments } from '@/api/department'
import { transListToTree } from '@/utils/index'
import { getEmployee, exportEmployee } from '@/api/employee'
import FileSaver from 'file-saver'
export default {
  name: 'Employee',
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      total: 0,
      queryParams: {
        queryParamsId: null,
        page: 1,
        pagesize: 5,
        keyword: ''
      },
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }]
    }
  },
  async created() {
    this.getDepartments()
  },
  methods: {
    async exportEmployee() {
      const res = await exportEmployee()
      // FileSaver.saveA(blob对象，文件名称)
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    // 设置模糊搜索。并做防抖处理
    changeFn() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployee()
      }, 600)
    },
    handleNodeClick(node) {
      // 点击的哪一个的id赋值给Id
      // 然后直接调用接口即可获取不同部门
      // console.log(node.id)
      this.queryParams.queryParamsId = node.id
      // console.log(this.queryParams.queryParamsId)
      // 当我们切换部门时，下面的页面需要变为1，否则请求会出问题
      this.queryParams.page = 1
      this.getEmployee()
    },
    async getDepartments() {
      // 获取树形结构
      const res = await getDepartments()
      this.depts = transListToTree(res, 0)
      this.queryParams.queryParamsId = this.depts[0].id
      // 因为获取数据是异步的，我们想要首个高亮，那么就需将首个高亮配置设为异步
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.queryParams.queryParamsId)
      })
      this.getEmployee()
    },
    async getEmployee() {
      const { rows, total } = await getEmployee({ ...this.queryParams })
      this.tableData = rows
      this.total = total
    }
  }

}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
